import type { VbenFormProps } from '@vben-core/form-ui';

import type { TableType } from '#/views/onlineDesign/designPage/template/table/type/TableType';

import { commonMethod, isRules, toCamelCase } from './commonMethod';

/**
 * 默认数据生成查询表单
 * @param fields
 * @param config 配置JSON
 */
export const defaultToGenerateSelectForm = async (
  fields: Array,
  config: any,
): VbenFormProps => {
  return fields
    .filter(
      ({ isHandle, fieldSelect, componentName }) =>
        isHandle &&
        fieldSelect === 1 &&
        componentName !== 'SparkUploadImg' &&
        componentName !== 'SparkEditor' &&
        componentName !== 'SparkUploadFile',
    )
    .map((field) => {
      const camelCaseLabel = toCamelCase(field.fieldName);
      // 公共配置
      const commonProps = {
        label: field.fieldAnnotation,
        fieldName: camelCaseLabel,
        extra: {
          showSelect: true,
          fieldRelevanceMode: field.fieldRelevanceMode,
          fieldPackagePath: field.fieldPackagePath,
        },
      } as TableType.FormComponent;
      return commonMethod.getComponent(field, commonProps, true, config);
    });
};

/**
 * 默认数据生成表单
 * @param fields
 * @param config 配置JSON
 */
export const defaultToGenerateForm = async (
  fields: Array,
  config: any,
): VbenFormProps => {
  return fields
    .filter(({ isHandle, fieldName }) => isHandle || fieldName === 'pid')
    .map((field, index) => {
      const camelCaseLabel = toCamelCase(field.fieldName);
      // 公共配置
      const commonProps = {
        label: field.fieldAnnotation,
        fieldName: camelCaseLabel,
        rules: isRules(field),
        extra: {
          showSelect: true,
          fieldRelevanceMode: field.fieldRelevanceMode,
          fieldPackagePath: field.fieldPackagePath,
        },
        sequence: index + 1,
      } as TableType.FormComponent;
      return commonMethod.getComponent(field, commonProps, false, config);
    });
};
